package L617;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: javase
 * @description: 最长无重复子串长度
 * @author: luolidong
 * @create: 2021-06-17 22:35
 * @version: 1.0
 */
public class Demo3 {
//    abcacd
//    a 0
//    b 1
//    c 2

    public int lengthOfLongestSubstring(String s) {
        if (s.length() == 1) return 1;
        Map<Character, Integer> map = new HashMap<>();
        int max = 1;
        int start = -1;
        for (int i = 0; i < s.length(); i++) {
            if (map.containsKey(s.charAt(i))) {
                start = Math.max(start, map.get(s.charAt(i)));
            }

            map.put(s.charAt(i), i);
            max = Math.max(max, i - start);
        }
        return max;
    }
}
